package com.youtui.manager.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.service.base.impl.BaseServiceImpl;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.NextIdUtil;
import com.youtui.manager.entity.PredictIncomeEntity;
import com.youtui.manager.mapper.PredictIncomeMapper;
import com.youtui.manager.model.Developer;
import com.youtui.manager.model.PredictIncome;
import com.youtui.manager.model.PredictIncomeExample;
import com.youtui.manager.model.PredictIncomeExample.Criteria;
import com.youtui.manager.service.IDeveloperService;
import com.youtui.manager.service.IPredictIncomeService;

@Service
public class PredictIncomeServiceImpl extends BaseServiceImpl implements IPredictIncomeService {

	@Autowired
	PredictIncomeMapper predictIncomeMapper;

	@Autowired
	IDeveloperService developerService;

	@Override
	public PageInfo<PredictIncomeEntity> findPredictIncomeEntityPageList(String appName, Date dealdate, int page,
			int rows) {
		PageHelper.startPage(page, rows);
		List<PredictIncomeEntity> list = findPredictIncomeEntityList(appName, dealdate);
		PageInfo<PredictIncomeEntity> pageinfo = new PageInfo<>(list);
		return pageinfo;
	}

	/**
	 * 查询预计收入列表
	 */
	@Override
	public PageInfo<PredictIncomeEntity> findPredictIncomeEntityPageList(Long devId, int page, int rows) {
		PageHelper.startPage(page, rows, false);
		List<PredictIncomeEntity> list = findPredictIncomeEntityList(devId, null, null);
		PageInfo<PredictIncomeEntity> pageinfo = new PageInfo<>(list);
		return pageinfo;
	}
	
	public List<PredictIncomeEntity> findPredictIncomeEntityList_7(Long devId , Date startDate , Date endDate){
		Map<String, Object> map = new HashMap<String, Object>();
		if (devId != null) {
			map.put("devId", devId);
		}
		if(startDate != null){
			map.put("startDate", startDate);
		}
		if(endDate != null){
			map.put("endDate", endDate);
		}  
		
		map.put("orderby", "pre.id esc");
		List<PredictIncomeEntity> list = predictIncomeMapper.selectPredictIncomeEntityList(map);
		return list;
	}


	@Override
	public List<PredictIncomeEntity> findPredictIncomeEntityList(String appName, Date dealdate) {
		return findPredictIncomeEntityList(null, appName, dealdate);
	}

	@Override
	public List<PredictIncomeEntity> findPredictIncomeEntityList(Long devId, String appName, Date dealdate) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (devId != null) {
			map.put("devId", devId);
		}

		if (StringUtils.isNotBlank(appName)) {
			map.put("appName", "%" + appName + "%");
		}
		if (dealdate != null) {
			map.put("dealdate", dealdate);
		}

		List<PredictIncomeEntity> list = predictIncomeMapper.selectPredictIncomeEntityList(map);
		return list;
	}

	@Override
	public int selectPredictIncomeCount(String appKey, Date curdate) {

		PredictIncomeExample example = new PredictIncomeExample();
		Criteria criteria = example.createCriteria();
		criteria.andAppKeyEqualTo(appKey);
		criteria.andCurdateEqualTo(curdate);
		return predictIncomeMapper.countByExample(example);
	}

	@Override
	public int insertPredictIncome(String[] s, Long excId) {

		// s[0] 日期 s[1] appname s[2] appkey s[3]uv s[4] 预计收益 s[5] 可提现收益

		PredictIncome income = new PredictIncome();
		income.setId(NextIdUtil.getNextId());
		income.setExcId(excId);
		income.setCurdate(DateUtils.convert2Date(s[0], DateUtils.DF_SHORT_STR));
		income.setAppName(s[1]);
		income.setAppKey(s[2]);
		income.setUv(Double.valueOf(s[3]).intValue());
		income.setPredict(new BigDecimal(s[4]));
		income.setStatus(1);
		income.setDealTime(new Date());

		Developer dev = developerService.findDeveloperByAppkey(income.getAppKey());
		if (dev == null) {
			income.setStatus(0);
			income.setRemark("appkey查询开发者信息失败");
		} else {
			income.setDevId(dev.getId());
		}

		int count = selectPredictIncomeCount(income.getAppKey(), income.getCurdate());
		if (count > 0) {
			income.setStatus(0);
			income.setRemark("重复导入");
		}

		predictIncomeMapper.insert(income);

		// 返回成功插入条数
		if (income.getStatus() == 1) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public PredictIncome findYesterPredictIncome(Long devId) {

		Date yesterday = DateUtils.setStartDay(DateUtils.addDays(new Date(), -1));

		PredictIncomeExample example = new PredictIncomeExample();
		Criteria criteria = example.createCriteria();
		criteria.andDevIdEqualTo(devId);
		criteria.andCurdateEqualTo(yesterday);
		criteria.andStatusEqualTo(SystemConstant.status_effect);
		List<PredictIncome> list = predictIncomeMapper.selectByExample(example);

		if (list.size() > 0) {
			return list.get(0);
		}
		return null;

	}

	@Override
	public BigDecimal findMonthPredictIncome(Long devId) {
		BigDecimal monthIncome = new BigDecimal(0);
		PredictIncomeExample example = new PredictIncomeExample();
		Criteria criteria = example.createCriteria();
		criteria.andDevIdEqualTo(devId);
		criteria.andCurdateGreaterThanOrEqualTo(DateUtils.getFirstDayOfMonth(new Date()));
		criteria.andStatusEqualTo(SystemConstant.status_effect);
		List<PredictIncome> list = predictIncomeMapper.selectByExample(example);

		if (list.size() > 0) {
			for (PredictIncome pre : list) {
				monthIncome = monthIncome.add(pre.getPredict());
			}
		}
		return monthIncome;
	}

}
